package com.hs.sfzy.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hs.sfzy.entity.PsychReply;
import com.hs.sfzy.entity.User;
import com.hs.sfzy.mapper.PsychMapper;
import com.hs.sfzy.mapper.PsychReplyMapper;
import com.hs.sfzy.mapper.UserMapper;
import com.hs.sfzy.service.PsychReplyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PsychReplyServiceImpl implements PsychReplyService {
    @Autowired
    private PsychReplyMapper psychReplyMapper;
    @Autowired
    private PsychMapper psychMapper;
    @Override
    public PageInfo<PsychReply> getAll(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<PsychReply> all = psychReplyMapper.getAll();
        PageInfo<PsychReply> pageInfo = new PageInfo<>(all);
        return pageInfo;
    }

    @Override
    public PageInfo<PsychReply> getTow(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<PsychReply> all = psychReplyMapper.getTow();
        PageInfo<PsychReply> pageInfo = new PageInfo<>(all);
        return pageInfo;
    }

    @Autowired
    private UserMapper userMapper;
    @Override
    public List<PsychReply> getReplyByPsychId(Integer id) {
        List<PsychReply> replyByPsychId = psychReplyMapper.getReplyByPsychId(id);
        for (PsychReply psychReply : replyByPsychId) {
            int userId = psychReply.getUserId();
            psychReply.setUser(userMapper.findUserById(userId));
        }
        return replyByPsychId;
    }
        @Override
        public void deleteByPsychId(int psychId) {
            // 调用Mapper的批量删除方法（对应ReplyMapper.xml中的SQL）
            psychReplyMapper.deleteByPsychId(psychId);
        }
    public int addReply(PsychReply psychReply) {
        int userId = psychReply.getUserId();
        User user = userMapper.findUserById(userId);
        user.setPoints(user.getPoints() + 10);
        userMapper.updateUser(user);
        return psychReplyMapper.addReply(psychReply);
    }


}
